<?php
class ModelCatalogExtProduct extends Model {
	public function getProducts($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "ext_product ";
			
			if (!empty($data['filter_name'])) {
				$sql .= " WHERE LCASE(`name`) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
			}
			
			// if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			// 	$sql .= " AND p.status = '" . (int) $data['filter_status'] . "'";
			// }
			
			$sort_data = array( 
				'name', 				
				'sort_order' 
			);
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			}
			else {
				$sql .= " ORDER BY name";
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			}
			else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}
				
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
				
				$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
			}
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		}
		// else {
		// 	$product_data = $this->cache->get('ext_product.' . (int) $this->config->get('config_language_id') . '.' . (int) $this->config->get('config_store_id'));
			
		// 	if (!$product_data) {
		// 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int) $this->config->get('config_language_id') . "' ORDER BY pd.name ASC");
				
		// 		$product_data = $query->rows;
				
		// 		$this->cache->set('product.' . (int) $this->config->get('config_language_id') . '.' . (int) $this->config->get('config_store_id'), $product_data);
		// 	}
			
		// 	return $product_data;
		// }
	}

	public function getProByPid($pid) {
		$sql="SELECT * FROM " . DB_PREFIX . "ext_product ";
		if($pid>=0)$sql.=" WHERE parent_id=".$pid;
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->rows;
	}
	
	public function getProByMid($mid){
		$sql="SELECT * FROM " . DB_PREFIX . "ext_product ";
		if($mid>=0)$sql.=" WHERE mid=".$mid;
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->rows;
	}
}